File manager - Edit - /home/autoph/public_html/projects/tickets-autohub/app/Console/Commands/SyncEmployees.php
Back
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; class SyncEmployees extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'sync:employees'; /** * The console command description. * * @var string */ protected $description = 'Synchronize employees form vts_user to main db users'; /** * Execute the console command. * * @return int */ public function __construct() { parent::__construct(); } public function handle() { $this->info('SyncEmployees command started.'); $data =[]; $employees = DB::connection('autohub_connect')->table('vts_users') ->select('employee_id','company','dealer','dept_id','u_name','u_fname','u_lname','u_position','email','u_status','usr_mobile', DB::raw('vts_company.comp_code as comp_code'),DB::raw('vts_dealerships.deal_code as dealer_code')) ->join('vts_company','vts_users.company','=','vts_company.comp_id') ->join('vts_dealerships','vts_users.dealer','=','vts_dealerships.deal_id') ->whereNotIn('employee_id', function($query){ $query->select('asa_id')->from(DB::connection('mysql')->getDatabaseName() . '.users'); }) ->where('u_status',1) // ->limit(5) // ->get(); ->orderBy('employee_id') ->chunk(100, function ($employees) { DB::beginTransaction(); try { foreach ($employees as $row) { try{ DB::connection('mysql')->table('users') ->insert([ 'asa_id' => $row->employee_id, 'f_name' => $row->u_fname, 'l_name' => $row->u_lname, 'email' => $row->email, 'password' => bcrypt('password'), 'position' => strtoupper($row->u_position), 'department_id' => $row->dept_id, 'viber' => $row->usr_mobile, 'deal_code' => $row->dealer_code, 'comp_code' => $row->comp_code, 'active' => 1, 'created_at' => now(), 'updated_at' => now(), ]); } catch(\Illuminate\Database\QueryException $e) { // $this->warn("Skipped duplicate: {$row->email}"); DB::connection('mysql')->table('users_copy') ->insert([ 'asa_id' => $row->employee_id, 'f_name' => $row->u_fname, 'l_name' => $row->u_lname, 'email' => $row->email, 'password' => bcrypt('password'), 'position' => strtoupper($row->u_position), 'department_id' => $row->dept_id, 'viber' => $row->usr_mobile, 'deal_code' => $row->dealer_code, 'comp_code' => $row->comp_code, 'active' => 1, 'created_at' => now(), 'updated_at' => now(), ]); } } DB::commit(); } catch (\Exception $e) { DB::rollback(); $this->error('Error: ' . $e->getMessage()); return Command::FAILURE; } }); return Command::SUCCESS; } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0 |
proxy
|
phpinfo
|
Settings